package org.wk.common.api.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wk.common.service.UploadService;
import org.wk.core.common.view.ResponseVo;

import java.util.Map;

/**
 * 上传文件
 * @author df.wu
 */
@RestController
@RequestMapping("/upload")
public class UploadController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UploadService uploadService;


    /**
     *
     * @param files
     * @param contentType image,audio,video,document
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/{contentType}", method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String image(MultipartFile files, @PathVariable("contentType") String contentType) throws Exception {
        logger.info("上传文件");

        Map<String, String> resMap = uploadService.doUpload(files, contentType);
        ResponseVo responseVo = new ResponseVo().success();
        responseVo.setData(resMap);
        String str = responseVo.toString();
        logger.info("上传文件返回数据：{}",str);
        return str;
    }

    /**
     * @Description 抓取网络图片
     * @Param [files, contentType]
     * @author ZhaoPing'an
     * @Date 2018/6/6 15:41
     */
    @RequestMapping(value = "/uploadByUrl/{contentType}", method=RequestMethod.GET)
    public String uploadImageByUrl(@PathVariable("contentType") String contentType,@RequestParam("url") String url) throws Exception {
        logger.info("抓取网络图片");
        Map<String, String> resMap = uploadService.uploadImageByUrl(url, contentType);
        ResponseVo responseVo = new ResponseVo().success();
        responseVo.setData(resMap);
        String str = responseVo.toString();
        logger.info("抓取网络图片返回数据：{}",str);
        return str;
    }


}
